// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "category_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ]
  },
  "shop_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ]
  },
  "goods_sn": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "货号",
    "label": "货号"
  },
  "name": {
    "rules": [
      {
        "required": true,
        "errorMessage": ""
      },
      {
        "format": "string"
      },
      {
        "minLength": 2,
        "maxLength": 30,
        "errorMessage": "商品名称长度为2-30个汉字或字符"
      }
    ],
    "title": "名称",
    "label": "名称"
  },
  "feature": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 50,
        "errorMessage": "商品特色长度为50个汉字或字符以内"
      }
    ],
    "title": "特色",
    "label": "特色"
  },
  "ingredients": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 50,
        "errorMessage": "商品主料长度为50个汉字或字符以内"
      }
    ],
    "title": "主料",
    "label": "主料"
  },
  "accessories": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 50,
        "errorMessage": "商品辅料长度为50个汉字或字符以内"
      }
    ],
    "title": "辅料",
    "label": "辅料"
  },
  "month_sales": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 0
      }
    ],
    "title": "月销量",
    "defaultValue": 0,
    "label": "月销量"
  },
  "total_sales": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 0
      }
    ],
    "title": "总销量",
    "defaultValue": 0,
    "label": "总销量"
  },
  "description": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "详细描述",
    "label": "详细描述"
  },
  "images": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      },
      {
        "maxLength": 50
      }
    ],
    "title": "商品照片",
    "label": "商品照片"
  },
  "goods_options": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "object"
      }
    ]
  },
  
  "weight": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "商品重量",
    "label": "商品重量"
  },
  "initial_purchase": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "起购数量",
    "defaultValue": 1,
    "label": "起购数量"
  },
  
  
  "comment_total": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "评价总数",
    "defaultValue": 0,
    "label": "评价总数"
  },
  
  "is_shelf": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否上架，默认true",
    "defaultValue": true,
    "label": "是否上架，默认true"
  },
  "create_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "创建时间"
  }
}

const enumConverter = {}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
